package com.mdinic.blogovi.rest;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mdinic.blogovi.model.BlogPost;
import com.mdinic.blogovi.model.BlogType;

@RequestMapping("/pbp")
@Controller
public class PublicBlogPostCtrl {

    @RequestMapping(value = "home", produces = "application/json")
    public @ResponseBody
    List<BlogPost> homePage(@RequestParam(value = "f", required = true) Integer firstResult,
            @RequestParam(value = "m", required = true) Integer maxResults,
            @RequestParam(value = "t", required = false) Long typeId,
            @RequestParam(value = "uid", required = false) String uid) {

        if (typeId == null) {
            return BlogPost.findPromotedApprovedBlogPostEntries(firstResult, maxResults);
        } else {
            BlogType type = BlogType.findBlogType(typeId);
            return BlogPost.findPromotedApprovedBlogPostEntries(firstResult, maxResults, type);
        }
    }

    @RequestMapping(value = "new", produces = "application/json")
    public @ResponseBody
    List<BlogPost> newLinksPage(@RequestParam(value = "f", required = true) Integer firstResult,
            @RequestParam(value = "m", required = true) Integer maxResults,
            @RequestParam(value = "t", required = false) Long typeId,
            @RequestParam(value = "uid", required = false) String uid) {

        if (typeId == null) {
            return BlogPost.findNotPromotedApprovedBlogPostEntries(firstResult, maxResults);
        } else {
            BlogType type = BlogType.findBlogType(typeId);
            return BlogPost.findNotPromotedApprovedBlogPostEntries(firstResult, maxResults, type);
        }
    }
}
